//
//  HJBaseNavController.swift
//  zhangmiSinweibo
//
//  Created by zhangmi on 16/5/21.
//  Copyright © 2016年 Paramount Pictures. All rights reserved.
//

import UIKit

class HJBaseNavController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //解决右滑手势的问题
        //存在的bug, 在最顶级的页面, 右滑返回手势还会存在
        self.interactivePopGestureRecognizer?.delegate = self
        
//        navigationController?.navigationBar.setBackgroundImage(UIImage(named: "NavBar"), forBarMetrics: .Default)
        
    }
    
    //重写push方法
    //HJBaseNavController(rootViewController: vc) ==> 内部会进行push操作 ==> 压栈
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //变量名称 和方法名称, 最好不要重名
        //判断是否是顶级页面
        var backtitle = "返回"
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1 {
                //设置返回按钮的title = 顶级页面的title
                backtitle = childViewControllers.first?.title ?? "返回"
            }
            
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: "返回", target: self, action: #selector(popToFontVC))
            
            viewController.navigationItem.leftBarButtonItem = back
            
            //隐藏底部的tabbar
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func popToFontVC() {
        //pop
        popViewControllerAnimated(true)
        
    }
    
    
    

    //实现手势协议方法
    //一般协议方法返回值为 bool类型, 表示如果返回true就表示对象能够正常工作
    //如果返回false 对象就不能正常工作
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
//        if childViewControllers.count > 1 {
//            //计入子页面
//            return true
//        }
        return childViewControllers.count > 1
    }

}
